package com.ketan.chameleonmuseum

import android.os.Bundle
import android.util.TypedValue
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.ketan.chameleonmuseum.ui.restaurant.RestaurantFragment
import com.ketan.chameleonmuseum.ui.home.HomeFragment
import com.ketan.chameleonmuseum.ui.course.CourseFragment
import com.ketan.chameleonmuseum.ui.mine.MineFragment
import com.uxhuanche.ui.base.KKFragmentPagerAdapter
import com.uxhuanche.ui.base.KnifePageChangeListener


class MainActivity : AppCompatActivity() {

    var mMenuItem: MenuItem? = null
    lateinit var navView: BottomNavigationView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        navView = findViewById(R.id.nav_view)
        supportActionBar?.hide()
        navView.itemIconTintList = null
        adjustNavigationIcoSize(navView)
        initViewPager(navView)
    }


    private fun adjustNavigationIcoSize(navigation: BottomNavigationView) {

        val menuView = navigation.getChildAt(0) as BottomNavigationMenuView
        for (i in 0 until menuView.childCount) {
            val iconView: View = menuView.getChildAt(i).findViewById(R.id.icon)
            val layoutParams = iconView.layoutParams as ViewGroup.MarginLayoutParams
            val displayMetrics = resources.displayMetrics
            layoutParams.height =
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16f, displayMetrics).toInt()
            layoutParams.width =
                TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16f, displayMetrics).toInt()
            iconView.layoutParams = layoutParams
        }
    }

    private fun initViewPager(navView: BottomNavigationView) {
        val clsList = listOf(
            HomeFragment::class.java,
            RestaurantFragment::class.java,
            CourseFragment::class.java,
            MineFragment::class.java
        )
        val viewpager = findViewById<ViewPager>(R.id.nav_host_viewPager)
        val pagerAdapter = KKFragmentPagerAdapter(supportFragmentManager, clsList)
        viewpager.adapter = pagerAdapter
        viewpager.offscreenPageLimit = 3
        viewpager.addOnPageChangeListener(object : KnifePageChangeListener() {
            override fun onPageSelected(position: Int) {
                mMenuItem?.isChecked = false
                mMenuItem = navView.menu.getItem(position)
                mMenuItem?.isChecked = true
            }
        })

        navView.setOnNavigationItemSelectedListener { item ->
            val id = item.itemId
            val arraysId = setOf(
                R.id.navigation_home,
                R.id.navigation_restaurant,
                R.id.navigation_course,
                R.id.navigation_my
            )
            viewpager?.currentItem = arraysId.indexOf(id)
            mMenuItem = item
            false
        }

    }


}